<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
        v-model="queryInfo.customerName"
        style="width: 200px"
        placeholder="请输入顾客名字"
        suffix-icon="el-icon-position"
        class="mll-5"
      />
      <el-button class="mll-5" type="primary" @click="fetchDataFromApi">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline" /></el-button>
    </div>
    <el-card class="box-card">
      <el-table :data="consumerData" style="width: 100%">
        <el-table-column label="顾客ID" prop="customerId" />
        <el-table-column label="姓名" prop="customerName" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="性别" prop="customerSex" />
        <el-table-column label="电话号码" prop="customerPhone" label-width="100" />
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
          </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.customerId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <span class="demonstration" />
        <div style="padding: 10px 0; text-align: center;">
          <el-pagination
            :current-page="queryInfo.page"
            :page-sizes="[5, 8, 10, 20]"
            :page-size="queryInfo.pageNum"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <el-dialog title="顾客信息" :visible.sync="dialogFormVisible" width="50%">
        <el-form label-width="80px" size="small">
          <el-form-item label="客户账号">
            <el-input v-model="form.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="form.customerName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.customerPhone" autocomplete="off" />
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="form.customerSex" placeholder="请选择性别">
              <el-option label="男" value="M"></el-option>
              <el-option label="女" value="F"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSave">{{ isEditMode ? '修改' : '确定' }}</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getConsumersList, deleteConsumersById } from '@/api/consumers'
import service from '@/utils/request'
export default {
  data() {
    return {
      consumerData: [],
      search: '',
      pageNum: 1,
      pageSize: 5,
      total: 0,
      customerName: '',
      form: {},
      dialogFormVisible: false,
      fileList: [],
      isEditMode: false,
      queryInfo: {
        customerName:'',
        page: 1,
        pageSize: 10
      }
    }
  },
  // tableData,menuId,menuName,menuImage,menuPrice
  created() {
    this.fetchDataFromApi()
  },
  methods: {
    // handleSizeChange(pageSize) {
    //   console.log(pageSize)
    //   this.pageSize = pageSize
    //   this.fetchDataFromApi()
    // },
    // handleCurrentChange(pageNum) {
    //   console.log(pageNum)
    //   this.pageNum = pageNum
    //   this.fetchDataFromApi()
    // },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.fetchDataFromApi()
    },
    // 当前页码的改变
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.fetchDataFromApi()
    },
    async fetchDataFromApi() {
      const result = await getConsumersList(this.queryInfo)
        if (result.code == 200) {
        this.consumerData = result.data.rows
        this.total = result.data.total
        console.log(result.data.rows)
      }
    },
    reset() {
      this.queryInfo.customerName = ''
      this.fetchDataFromApi()
    },
    handleSave() {
      if (this.isEditMode) {
        this.updata()
      } else {
        this.save()
      }
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.isEditMode = false
    },
    save() {
      service.post('http://localhost:9090/customer', this.form).then(res => {
        if (res) {
          this.$message.success('保存成功')
          this.dialogFormVisible = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('保存失败')
        }
      })
    },
    delete(customerId) {
      service.delete('http://localhost:9090/customer/' + customerId).then(res => {
        if (res) {
          this.$message.success('删除成功')
          this.delVisibleaa = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
      this.isEditMode = true
    },
    handleDelete(customerId) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(customerId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    updata() {
      service.put('http://localhost:9090/customer', this.form).then(res => {
        if (res) {
          this.$message.success('保存成功')
          this.dialogFormVisible = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('保存失败')
        }
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.mll-5 {
  margin-left: 10px;
}
</style>
